package com.crazyteam.genid.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.crazyteam.genid.mapper"}, sqlSessionFactoryRef = "primaryKeySessionFactory")
public class PrimaryMybatiseConfig {

    @Resource(name = "primaryKeyDataSource")
    private DataSource primaryKeyDataSource;

    @Bean
    public SqlSessionFactory primaryKeySessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(primaryKeyDataSource); // 使用omsLog数据源, 连接omsLog库
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/primarykey/PrimaryKeyMapper.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager primaryKeyTransactionManager(){
        return new DataSourceTransactionManager(primaryKeyDataSource);
    }
}
